package tencent;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import java.io.*;

public class QQCar {
	public static final String QQNUM = ""; //
	public static final String PASSWORD = ""; //

	private static DefaultHttpClient httpclient = new DefaultHttpClient();
	private static boolean loginFlag = false;
	private static List<Cookie> cookies;
	private static HttpResponse response;
	private static HttpGet request;
	private static String verifyString;
	private static String verifySession;

	private static void getVerifyImage() throws Exception {
		String url = "http://58.251.61.244/getimage?aid=8000108&0.7022592303274631";
		HttpGet httpget = new HttpGet(url);
		httpget.addHeader("Cookie", "");
		httpget.setHeader("Accept", "text/html, */*");
		httpget
				.addHeader("User-Agent",
						"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)");
		// httpget.addHeader("Connection", "close");
		// httpget.setHeader("User-Agent",
		// "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20")
		response = httpclient.execute(httpget);

		Header[] headers = response.getHeaders("Set-Cookie");// .getAllHeaders();

		for (Header h : headers) {
			System.out.println(h);
		}
		verifySession = headers[0].getValue().split(";|=")[1];

		InputStream in = response.getEntity().getContent();
		File verifyFile = new File(QQCar.class.getResource("output").getPath()
				+ "/verifycode.jpg");
		FileOutputStream out = new FileOutputStream(verifyFile);
		byte[] buf = new byte[1024];
		while (in.read(buf) != -1) {
			out.write(buf);
		}
		out.flush();
		System.out.println("pic:" + verifyFile.getCanonicalPath());
		System.out.println("valify code:");
		verifyString = new BufferedReader(new InputStreamReader(System.in))
				.readLine();
		while (verifyString.length() != 4) {
			System.out.println("alify code error:");
			verifyString = new BufferedReader(new InputStreamReader(System.in))
					.readLine();
		}
	}

	public static void doLogin() throws Exception {
		HttpHost proxy = new HttpHost("172.20.230.5", 3128);  
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);  
		if (!loginFlag) {
			getVerifyImage();
			HttpPost httpost = new HttpPost("http://ptlogin2.qq.com/login");
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("u", QQNUM));
			nvps.add(new BasicNameValuePair("p", MD5Security.md5_3(PASSWORD)
					+ verifyString.toUpperCase()));

			nvps.add(new BasicNameValuePair("verifycode", verifyString));
			nvps.add(new BasicNameValuePair("aid", "8000108"));
			nvps.add(new BasicNameValuePair("u1",
					"http://imgcache.qq.com/qzone/v5/loginsucc.html"));
			nvps.add(new BasicNameValuePair("fp", "loginerroralert"));
			nvps.add(new BasicNameValuePair("h", "1"));
			nvps.add(new BasicNameValuePair("ptredirect", "0"));
			nvps.add(new BasicNameValuePair("ptlang", "0"));
			nvps.add(new BasicNameValuePair("from_ui", "1"));
			nvps.add(new BasicNameValuePair("dumy", "1"));

			httpost
					.setHeader("User-Agent",
							"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)");
			httpost.setHeader("Cookie", "verifysession=" + verifySession + ";");
			httpost
					.setHeader(
							"Referer",
							"http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&target=self&appid=8000108&qlogin_jumpname=vipmyqq&f_url=loginerroralert&qlogin_auto_login=1&s_url=http%3A//imgcache.qq.com/qzone/v5/loginsucc.html&qlogin_param=jump_url%3D");
			httpost.setHeader("Accept", "text/html, */*");
			httpost.removeHeaders("Cookie2");
			httpost.removeHeaders("Expect");
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
			response = httpclient.execute(httpost);
			cookies = httpclient.getCookieStore().getCookies();
			if (cookies.size() < 3) {
				System.err.println("failed...");
				loginFlag = false;
			} else {
				System.out.println("succeed...");
				for (Cookie c : cookies) {
					System.out.println(c);
				}
				loginFlag = true;
			}
		}

	}

	public static void main(String[] args) throws Exception {
		doLogin();
	}
}
